//
//  DJDVideoBottomView.m
//  HCVideoModule
//
//  Created by 蒋千秋 on 2021/9/23.
//

#import "DJDVideoBottomView.h"
#import <HCGitModule/IGModuleHeader.h>
#import <HCBaseModule/HCLabelCCell.h>
#import <HCBaseModule/HCImgViewLabelCCell.h>
#import <ReactiveObjC.h>
#import <TXLiteAVSDK_TRTC/TRTCCloud.h>

#import <MBProgressHUD+BJHubHelper.h>
#import <Masonry/Masonry.h>
#import <HCBaseModule/HCConstant.h>
#define kLRSpace 5
@interface DJDVideoBottomView()
@property (nonatomic,strong)IGSubListSectionController *timerSC;
@property (nonatomic,assign)TRTCAudioRoute audioRoute;
@end
@implementation DJDVideoBottomView
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.IGCollectionView.backgroundColor = [UIColor clearColor];
        self.IGCollectionView.showsVerticalScrollIndicator = NO;
        [self addSubview:self.IGCollectionView];
        [self.IGCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(0);
        }];
        [self confiureData];
        self.IGAdapter.collectionView = self.IGCollectionView;
        self.IGAdapter.dataSource = self;
        self.audioRoute = TRTCAudioModeSpeakerphone;
    }
    return self;
}
- (void)confiureData{

    {
        NSObject *obj = NSObject.new;
        IGSubListSectionController *SC = [[IGSubListSectionController alloc]initWithCellNumberOfItemsBlock:nil dequeueCellBlock:^UICollectionViewCell * _Nullable(id  _Nullable item, IGListSectionController * _Nonnull sectionController, NSIndexPath * _Nonnull indexPath) {
            HCLabelCCell *cell = [sectionController.collectionContext dequeueReusableCellWithNibName:@"HCLabelCCell" bundle:nil forSectionController:sectionController atIndex:indexPath.item];
            cell.contentView.backgroundColor = UIColor.clearColor;
            cell.label.text = @"";
            cell.label.textColor = UIColor.whiteColor;
            cell.label.textAlignment = NSTextAlignmentCenter;
            return cell;
        } sizeBlock:^CGSize(id  _Nonnull item, IGListSectionController * _Nonnull sectionController, NSIndexPath * _Nonnull indexPath) {
            CGFloat width = sectionController.collectionContext.containerSize.width;
            return CGSizeMake(width, 70);
        } didSelectItemBlock:nil];
        obj.IGSectionController = SC;
        self.timerSC = SC;
        [self.IGDatas addObject:obj];
    }
    
    {
        CGFloat minimumInteritemSpacing = 10;
        CGFloat minimumLineSpacing = 10;
        int colum = 3;
        NSArray *titles = @[@"免提",@"挂断",@"切换摄像头"];
        NSArray *images = @[@"mtg",@"guaduan",@"qhsxt"];
        HCWeakSelf
        IGSubListSectionController *SC = [[IGSubListSectionController alloc] initWithCellNumberOfItemsBlock:^NSInteger(id  _Nonnull item, IGListSectionController * _Nonnull sectionController) {
            return titles.count;
        } dequeueCellBlock:^UICollectionViewCell * _Nullable(id  _Nullable item, IGListSectionController * _Nonnull sectionController, NSIndexPath * _Nonnull indexPath) {
            HCImgViewLabelCCell *cell = [sectionController.collectionContext dequeueReusableCellWithNibName:@"HCImgViewLabelCCell" bundle:nil forSectionController:sectionController atIndex:indexPath.item];
            cell.backgroundColor = UIColor.clearColor;
            cell.label.text = titles[indexPath.item];
            cell.label.textColor = UIColor.whiteColor;
            cell.label.superview.backgroundColor = UIColor.clearColor;
            cell.imgView.image = [UIImage imageNamed:images[indexPath.item]];
            cell.labelTopCon.constant = 6;
            if (indexPath.item == 0) cell.userInteractionEnabled = NO;
            return cell;
        } sizeBlock:^CGSize(id  _Nonnull item, IGListSectionController * _Nonnull sectionController, NSIndexPath * _Nonnull indexPath) {
            CGFloat width = (sectionController.collectionContext.containerSize.width - kLRSpace * 2 - minimumInteritemSpacing * colum) / colum;
            return CGSizeMake(width, 120);
        } didSelectItemBlock:^(id  _Nullable item, IGListSectionController * _Nonnull sectionController, NSIndexPath * _Nonnull indexPath) {
            if (indexPath.item == 1) {
                [TRTCCloud.sharedInstance exitRoom];
                [MBProgressHUD bjHideHubInView:weakSelf.vc.view];
            }else if(indexPath.item == 2){
                [TRTCCloud.sharedInstance switchCamera];
            }else{
                HCImgViewLabelCCell *cell = [sectionController.collectionContext cellForItemAtIndex:0 sectionController:sectionController];
                if (weakSelf.audioRoute == TRTCAudioModeSpeakerphone) {
                    [TRTCCloud.sharedInstance setAudioRoute:TRTCAudioModeEarpiece];
                    weakSelf.audioRoute = TRTCAudioModeEarpiece;
                    cell.imgView.image = [UIImage imageNamed:@"mtk"];
                }else{
                    [TRTCCloud.sharedInstance setAudioRoute:TRTCAudioModeSpeakerphone];
                    weakSelf.audioRoute = TRTCAudioModeSpeakerphone;
                    cell.imgView.image = [UIImage imageNamed:@"mtg"];
                }
            }
        }];
        SC.inset = UIEdgeInsetsMake(0, kLRSpace, 0, kLRSpace);
        SC.minimumInteritemSpacing = minimumInteritemSpacing;
        SC.minimumLineSpacing = minimumLineSpacing;
        NSObject *obj = NSObject.new;
        obj.IGDiffId = @"section2";
        obj.IGSectionController = SC;
        [self.IGDatas addObject:obj];
    }
}

- (void)onUserVideoAvailable:(NSString *)userId available:(BOOL)available{
    if (available) {
        __block NSInteger time = 0;
        NSObject *obj = self.IGDatas.lastObject;
        HCImgViewLabelCCell *cell = [obj.IGSectionController.collectionContext cellForItemAtIndex:0 sectionController:obj.IGSectionController];
        cell.userInteractionEnabled = YES;
        HCWeakSelf
        [[[RACSignal interval:1 onScheduler:RACScheduler.mainThreadScheduler] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(NSDate * _Nullable x) {
            time += 1;
            int  mins = time / 60;
            int  seconds = time % 60;
            HCLabelCCell *cell =  [weakSelf.timerSC.collectionContext cellForItemAtIndex:0 sectionController:weakSelf.timerSC];
            cell.label.text = [NSString stringWithFormat:@"%02d:%02d",mins,seconds];
        }];
    }
}
- (void)onError:(TXLiteAVError)errCode errMsg:(nullable NSString *)errMsg extInfo:(nullable NSDictionary*)extInfo{
  
}
- (void)onRemoteUserLeaveRoom:(NSString *)userId reason:(NSInteger)reason{
}
@end
